package org.apache.mina.example.http;

import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.http.HttpRequestImpl;
import org.apache.mina.http.api.DefaultHttpResponse;
import org.apache.mina.http.api.HttpEndOfContent;
import org.apache.mina.http.api.HttpMethod;
import org.apache.mina.http.api.HttpRequest;
import org.apache.mina.http.api.HttpVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyHttpHandler extends IoHandlerAdapter
{
    Logger log = LoggerFactory.getLogger(getClass());

    public void sessionCreated(IoSession session) throws Exception {
        log.info("sessionCreated");
    }

    public void sessionOpened(IoSession session) throws Exception {
//        log.info("sessionOpened");
//        Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//        Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
//        Accept-Encoding:gzip,deflate,sdch
//        Accept-Language:zh-CN,zh;q=0.8
//        Connection:keep-alive
//        Cookie:USERID=74609e6c8064168ebcd3bc3368; BAIDU_WAP_WENKU=a6def3360b4c2e3f572763c4_6_1_1000_2_7_1_color_wk; BAIDU_WISE_UID=frontui_1289811007_4852; BAIDUID=D9ED4CE7E371BAA69FDA5311B379251C:FG=1; BDREFER=%7Burl%3A%22http%3A//news.baidu.com/%22%2Cword%3A%22%22%7D; bdshare_firstime=1359805000128; BDUSS=xmTHJzS2EwN3ZsNjRvM1FTWDNPQVdRbnZoVE5qVFYxV2JkaU1PRkhwTEtpM0JSQVFBQUFBJCQAAAAAAAAAAApBF-9g8kIba2lkZGluZzg3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAODKZHEAAAAAeGlDAAAAAAAxMC4zNi4xNMo9g1DKPYNQem; BDUT=nght966CBF37CB788DE54C5082E61CA58BBF1392a69eec71; MAP_NEWDATA1=V1; MCITY=-%3A; PMS_Cache=1366030252950
//        Host:www.baidu.com
//        User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22
        HttpVersion version=HttpVersion.HTTP_1_0;
        HttpMethod method =HttpMethod.GET;
        String requestedPath = "/";
        String queryString = "";
        Map<String ,String > headers= new HashMap<String, String>();
        headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        headers.put("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
        headers.put("Accept-Language", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        headers.put("Accept-Encoding", "gzip,deflate,sdch");
        headers.put("Connection", "USERID=74609e6c8064168ebcd3bc3368; BAIDU_WAP_WENKU=a6def3360b4c2e3f572763c4_6_1_1000_2_7_1_color_wk; BAIDU_WISE_UID=frontui_1289811007_4852; BAIDUID=D9ED4CE7E371BAA69FDA5311B379251C:FG=1; BDREFER=%7Burl%3A%22http%3A//news.baidu.com/%22%2Cword%3A%22%22%7D; bdshare_firstime=1359805000128; BDUSS=xmTHJzS2EwN3ZsNjRvM1FTWDNPQVdRbnZoVE5qVFYxV2JkaU1PRkhwTEtpM0JSQVFBQUFBJCQAAAAAAAAAAApBF-9g8kIba2lkZGluZzg3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAODKZHEAAAAAeGlDAAAAAAAxMC4zNi4xNMo9g1DKPYNQem; BDUT=nght966CBF37CB788DE54C5082E61CA58BBF1392a69eec71; MAP_NEWDATA1=V1; MCITY=-%3A; PMS_Cache=1366030252950");
        headers.put("Host", "www.baidu.com");
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22");
        HttpRequest request = new HttpRequestImpl(version, method, requestedPath, queryString, headers);
        session.write(request);
    }

    public void sessionClosed(IoSession session) throws Exception {
        session.close(false);
        log.info("sessionClosed");
    }

    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
//        log.info("sessionIdle");
    }

    public void messageReceived(IoSession session, Object message) throws Exception {
//        log.info("messageReceived");
//        if(message instanceof DefaultHttpResponse){
//            log.info(message.toString());
//        }else if(message instanceof HttpEndOfContent){
//            log.info(message.toString());
//        }else{
//        }
    }

    public void messageSent(IoSession session, Object message) throws Exception {
//        log.info("messageSent");
    }
}
